package com.leased.controller;

import com.leased.enums.BusinessStatusEnum;
import com.leased.handler.CUserHandler;
import com.leased.pojo.CUsers;
import com.leased.request.cuser.CUserAddRequest;
import com.leased.request.cuser.CUserListRequest;
import com.leased.response.CUserDetailResponse;
import com.leased.response.CUserResponse;
import com.leased.response.MyCarsResponse;
import com.leased.utils.APIResponse;
import com.leased.utils.Page;
import com.leased.utils.YCSystemStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by shencx on 2017/8/1.
 */
@Controller
@RequestMapping(value = "/api/cuser")
public class CUserController {

    final Logger LOG = LoggerFactory.getLogger(CUserController.class);

    @Resource
    private CUserHandler cUserHandler;

    /**
     * 车主信息列表
     * @param request
     * @param cUserListRequest
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public APIResponse<Page<CUserResponse>> list(HttpServletRequest request, CUserListRequest cUserListRequest){
        APIResponse<Page<CUserResponse>> apiResponse = new APIResponse<>();
        Page<CUserResponse> page = null;
        try {
            page = cUserHandler.getCUserPage(cUserListRequest);
            apiResponse.setStatus(BusinessStatusEnum.SUCCESS.getCode());
            apiResponse.setMsg(BusinessStatusEnum.SUCCESS.getDesc());
            apiResponse.setData(page);
        } catch (Throwable e) {
            LOG.error("获取车主用户信息列表发生异常", cUserListRequest);
            apiResponse.setStatus(YCSystemStatusEnum.SYSTEM_ERROR.getCode());
            apiResponse.setMsg(YCSystemStatusEnum.SYSTEM_ERROR.getDesc());
        }
        return apiResponse;
    }

    /**
     * 添加修改车主用户信息
     * @param request
     * @param cUserAddRequest
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public APIResponse edit(HttpServletRequest request, CUserAddRequest cUserAddRequest){
        APIResponse apiResponse = new APIResponse();
        try {
            cUserHandler.addCUserInfo(cUserAddRequest);
            apiResponse.setStatus(BusinessStatusEnum.SUCCESS.getCode());
            apiResponse.setMsg(BusinessStatusEnum.SUCCESS.getDesc());
        } catch (Throwable e) {
            LOG.error("添加修改车主用户信息发生异常", cUserAddRequest);
            apiResponse.setStatus(YCSystemStatusEnum.SYSTEM_ERROR.getCode());
            apiResponse.setMsg(YCSystemStatusEnum.SYSTEM_ERROR.getDesc());
        }
        return apiResponse;
    }

    /**
     * 获取车主用户详情
     * @param request
     * @param cUserListRequest
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/detail",method = RequestMethod.GET)
    public APIResponse<CUsers> detail(HttpServletRequest request,CUserListRequest cUserListRequest){
        APIResponse<CUsers> apiResponse = new APIResponse<>();
        CUsers cUsers = null;
        try {
            cUsers = cUserHandler.getCUserDetail(cUserListRequest);
            apiResponse.setStatus(BusinessStatusEnum.SUCCESS.getCode());
            apiResponse.setMsg(BusinessStatusEnum.SUCCESS.getDesc());
            apiResponse.setData(cUsers);
        } catch (Throwable e) {
            LOG.error("获取车主详情信息发生异常", cUserListRequest);
            apiResponse.setStatus(YCSystemStatusEnum.SYSTEM_ERROR.getCode());
            apiResponse.setMsg(YCSystemStatusEnum.SYSTEM_ERROR.getDesc());
        }
        return apiResponse;
    }

    /**
     * 获取车主明细
     * @param request
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/particulars",method = RequestMethod.GET)
    public APIResponse<CUserDetailResponse> particulars(HttpServletRequest request,Integer id){
        APIResponse<CUserDetailResponse> apiResponse = new APIResponse<>();
        CUserDetailResponse cUserDetailResponse = null;
        try {
            cUserDetailResponse = cUserHandler.getCUserParticulars(id);
            apiResponse.setStatus(BusinessStatusEnum.SUCCESS.getCode());
            apiResponse.setMsg(BusinessStatusEnum.SUCCESS.getDesc());
            apiResponse.setData(cUserDetailResponse);
        } catch (Throwable e) {
            LOG.error("根据主键获取车主详情信息发生异常", id);
            apiResponse.setStatus(YCSystemStatusEnum.SYSTEM_ERROR.getCode());
            apiResponse.setMsg(YCSystemStatusEnum.SYSTEM_ERROR.getDesc());
        }
        return apiResponse;
    }

    /**
     * 获取我的车队列表
     * @param request
     * @param cUserListRequest
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/cars_team",method = RequestMethod.GET)
    public APIResponse<Page<MyCarsResponse>> carsTeam(HttpServletRequest request,CUserListRequest cUserListRequest){
        APIResponse<Page<MyCarsResponse>> apiResponse = new APIResponse<>();
        Page<MyCarsResponse> page = null;
        try {
            page = cUserHandler.getMyCarsList(cUserListRequest);
            apiResponse.setStatus(BusinessStatusEnum.SUCCESS.getCode());
            apiResponse.setMsg(BusinessStatusEnum.SUCCESS.getDesc());
            apiResponse.setData(page);
        } catch (Throwable e) {
            LOG.error("获取我的车队列表发生异常", cUserListRequest);
            apiResponse.setStatus(YCSystemStatusEnum.SYSTEM_ERROR.getCode());
            apiResponse.setMsg(YCSystemStatusEnum.SYSTEM_ERROR.getDesc());
        }
        return apiResponse;
    }
}
